/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package portal.system.inzerat.service;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import portal.system.inzerat.Inzerat;

/**
 *
 * @author Riso-Desktop
 */
public class UploadInzeratService implements Runnable {

    private String driver = portal.constants.Database.DATABASE_DRIVER;
    private Connection con = null;
    private Statement stmt = null;
    private ResultSet rs = null;
    private String userName = portal.constants.Database.DATABASE_USER;
    private String password = portal.constants.Database.DATABASE_PASSWORD;
    private String URL = portal.constants.Database.DATABASE_URL;
    
    Inzerat inzerat;
    Thread vlakno;
    ArrayList<String> subory;

    public UploadInzeratService(Inzerat inzerat) {
        this.inzerat = inzerat;
        subory = new ArrayList<String>();
        vlakno = new Thread(this);
        pripojit();
    }
    
    private void pripojit(){
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(URL, userName, password);
        } catch (Exception ex) {
        }
    }

    public void odoslatInzerat() {
        vlakno.start();
    }
    
    
    private synchronized void vymazatUploadnuteObrazky(){
        boolean okej=false;
        for(String s :subory){
            File f = new File(s);
            System.out.println(f.getPath());
            okej = f.delete();
            
        }
        if(!okej){
            System.err.println("Nepodarilo sa vymazať obrázky");
        }else{
            System.out.println("obrazok vymazaný");
        }
    }

    private synchronized void ulozitInzerat() {
        try {
            stmt = con.createStatement();
            String sql = "insert into inzerat(meno,email,heslo, nazov_inzeratu,Cena,text_inzeratu,id_mesta,Adresa,id_podkategorie,datum_pridania)"
                    + " values ("+"'"+inzerat.getMeno()+"',"+"'"+inzerat.getEmail()+"',"+"'"+inzerat.getHeslo()+"',"+ "'"+inzerat.getNazov_inzeratu()+"',"+
                    "'"+inzerat.getCena()+"'," + "'"+inzerat.getText_inzeratu()+"',"+"1," +"'"+inzerat.getAdresa()+"',"+ "'"+inzerat.getId_podkategorie()+"',"+
                    "'"+inzerat.getDatum_pridania()+"')";
            System.out.println(sql);
            
            stmt.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS);
            rs = stmt.getGeneratedKeys();
            rs.next();
            inzerat.setId_inzeratu(rs.getInt(1));
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private synchronized void uploadDoDB() {
        try {
            for(int i = 0;i<subory.size();i++){
            PreparedStatement ps = con.prepareStatement("INSERT INTO obrazky_inzerat(id_inzeratu,obrazok) VALUES("+inzerat.getId_inzeratu()+",?)");
            File file = new File(subory.get(i));

            FileInputStream fs = new FileInputStream(file);
            
            ps.setBinaryStream(1,fs,fs.available());
            ps.executeUpdate();
            fs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

    private synchronized void ulozitObrazky() {
        try {

            File item;
            for (int i = 0; i < 5; i++) {
                switch (i) {
                    case 0:
                        item = inzerat.getObrazok1();
                        break;
                    case 1:
                        item = inzerat.getObrazok2();
                        break;
                    case 2:
                        item = inzerat.getObrazok3();
                        break;
                    case 3:
                        item = inzerat.getObrazok4();
                        break;
                    case 4:
                        item = inzerat.getObrazok5();
                        break;
                    default:
                        item = null;
                        break;
                }
                if(item!=null){
                    subory.add(item.getPath());
                }

            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public synchronized void run() {
        ulozitInzerat();
        try {
            vlakno.sleep(500);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        ulozitObrazky();
        uploadDoDB();
        try {
            vlakno.sleep(500);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        vymazatUploadnuteObrazky();
    }
}
